# Where is this and what do we need it for?
#INCLUDE(ListHandle)

MACRO(READ_GCC_VERSION)
if (CMAKE_COMPILER_IS_GNUCC)
   execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion
                   OUTPUT_VARIABLE GCC_VERSION)
   string(REGEX MATCHALL "[0-9]+" GCC_VERSION_COMPONENTS ${GCC_VERSION})
   list(GET GCC_VERSION_COMPONENTS 0 GCC_MAJOR)
#        list(GET GCC_VERSION_COMPONENTS 1 GCC_MINOR)
endif()
ENDMACRO(READ_GCC_VERSION)


macro(MacroEmptyExternalProject proj dependencies)

ExternalProject_Add(${proj}
 DOWNLOAD_COMMAND ""
 CONFIGURE_COMMAND ""
 BUILD_COMMAND ""
 INSTALL_COMMAND ""
 DEPENDS
   ${dependencies}
 )

endmacro()

MACRO(FILTER_OUT FILTERS INPUTS OUTPUT)
  # Mimicks Gnu Make's $(filter-out) which removes elements 
  # from a list that match the pattern.
  # Arguments:
  #  FILTERS - list of patterns that need to be removed
  #  INPUTS  - list of inputs that will be worked on
  #  OUTPUT  - the filtered list to be returned
  # 
  # Example: 
  #  SET(MYLIST this that and the other)
  #  SET(FILTS this that)
  #
  #  FILTER_OUT("${FILTS}" "${MYLIST}" OUT)
  #  MESSAGE("OUTPUT = ${OUT}")
  #
  # The output - 
  #   OUTPUT = and;the;other
  #
  SET(FOUT "")
  FOREACH(INP ${INPUTS})
      SET(FILTERED 0)
      FOREACH(FILT ${FILTERS})
          IF(${FILTERED} EQUAL 0)
              IF("${FILT}" STREQUAL "${INP}")
                  SET(FILTERED 1)
              ENDIF("${FILT}" STREQUAL "${INP}")
          ENDIF(${FILTERED} EQUAL 0)
      ENDFOREACH(FILT ${FILTERS})
      IF(${FILTERED} EQUAL 0)
          SET(FOUT ${FOUT} ${INP})
      ENDIF(${FILTERED} EQUAL 0)
  ENDFOREACH(INP ${INPUTS})
  SET(${OUTPUT} ${FOUT})
ENDMACRO(FILTER_OUT FILTERS INPUTS OUTPUT)


MACRO(GET_HEADERS_EXTENSIONLESS DIR GLOB_PATTERN OUTPUT)
       FILE(GLOB TMP "${DIR}/${GLOB_PATTERN}" )
 #FOREACH(F ${TMP})
       #    MESSAGE(STATUS "header-->${F}<--")
 #ENDFOREACH(F ${TMP})
       FILTER_OUT("${DIR}/CVS" "${TMP}" TMP)
       FILTER_OUT("${DIR}/cvs" "${TMP}" ${OUTPUT})
       FILTER_OUT("${DIR}/.svn" "${TMP}" ${OUTPUT})
ENDMACRO(GET_HEADERS_EXTENSIONLESS DIR GLOB_PATTERN OUTPUT)

MACRO(ADD_DIRS_TO_ENV_VAR _VARNAME )
FOREACH(_ADD_PATH ${ARGN}) 
FILE(TO_NATIVE_PATH ${_ADD_PATH} _ADD_NATIVE)
#SET(_CURR_ENV_PATH $ENV{PATH})
#LIST(SET _CURR_ENV_PATH ${_ADD_PATH})
#SET(ENV{PATH} ${_CURR_ENV_PATH})${_FILE}
IF(WIN32)
   SET(ENV{${_VARNAME}} "$ENV{${_VARNAME}};${_ADD_NATIVE}")
ELSE(WIN32)
   SET(ENV{${_VARNAME}} "$ENV{${_VARNAME}}:${_ADD_NATIVE}")
ENDIF(WIN32)
#MESSAGE(" env ${_VARNAME} --->$ENV{${_VARNAME}}<---")
ENDFOREACH(_ADD_PATH)
ENDMACRO(ADD_DIRS_TO_ENV_VAR _VARNAME )

#---------------------------------------------------
# MACRO CORRECT_PATH VAR PATH 
# corrects slashes in PATH to be cmake conformous ( / ) 
# and puts result in VAR 
#---------------------------------------------------

MACRO(CORRECT_PATH VAR PATH)
SET(${VAR} ${PATH})
IF(WIN32)    
STRING(REGEX REPLACE "/" "\\\\" ${VAR} "${PATH}")
ENDIF(WIN32)    
ENDMACRO(CORRECT_PATH)

MACRO(TARGET_LOCATIONS_SET_FILE FILE)
SET(ACCUM_FILE_TARGETS ${FILE})
FILE(WRITE ${ACCUM_FILE_TARGETS} "")
ENDMACRO(TARGET_LOCATIONS_SET_FILE FILE)

MACRO(TARGET_LOCATIONS_ACCUM TARGET_NAME)
IF(ACCUM_FILE_TARGETS)
IF(EXISTS ${ACCUM_FILE_TARGETS})
GET_TARGET_PROPERTY(_FILE_LOCATION ${TARGET_NAME} LOCATION)
FILE(APPEND ${ACCUM_FILE_TARGETS} "${_FILE_LOCATION};")
#SET(_TARGETS_LIST ${_TARGETS_LIST} "${_FILE_LOCATION}" CACHE INTERNAL "lista dll")
#MESSAGE("adding target -->${TARGET_NAME}<-- file -->${_FILE_LOCATION}<-- to list -->${_TARGETS_LIST}<--")
#SET(ACCUM_FILE_TARGETS ${ACCUM_FILE_TARGETS} ${_FILE_LOCATION})
ENDIF(EXISTS ${ACCUM_FILE_TARGETS})
ENDIF(ACCUM_FILE_TARGETS)
ENDMACRO(TARGET_LOCATIONS_ACCUM TARGET_NAME)

MACRO(TARGET_LOCATIONS_GET_LIST _VAR)
IF(ACCUM_FILE_TARGETS)
IF(EXISTS ${ACCUM_FILE_TARGETS})
 FILE(READ ${ACCUM_FILE_TARGETS} ${_VAR})    
ENDIF(EXISTS ${ACCUM_FILE_TARGETS})
ENDIF(ACCUM_FILE_TARGETS)
ENDMACRO(TARGET_LOCATIONS_GET_LIST _VAR)


MACRO(FIND_DEPENDENCY DEPNAME INCLUDEFILE LIBRARY SEARCHPATHLIST)

MESSAGE(STATUS "searching ${DEPNAME} -->${INCLUDEFILE}<-->${LIBRARY}<-->${SEARCHPATHLIST}<--")

SET(MY_PATH_INCLUDE )
SET(MY_PATH_LIB )
SET(MY_PATH_BIN )

FOREACH( MYPATH ${SEARCHPATHLIST} )
SET(MY_PATH_INCLUDE ${MY_PATH_INCLUDE} ${MYPATH}/include)
SET(MY_PATH_LIB ${MY_PATH_LIB} ${MYPATH}/lib)
SET(MY_PATH_BIN ${MY_PATH_BIN} ${MYPATH}/bin)
ENDFOREACH( MYPATH ${SEARCHPATHLIST} )

SET(MYLIBRARY "${LIBRARY}")
SEPARATE_ARGUMENTS(MYLIBRARY)

#MESSAGE( " include paths: -->${MY_PATH_INCLUDE}<--")

#MESSAGE( " ${DEPNAME}_INCLUDE_DIR --> ${${DEPNAME}_INCLUDE_DIR}<--")

FIND_PATH("${DEPNAME}_INCLUDE_DIR" ${INCLUDEFILE}
${MY_PATH_INCLUDE}
)
MARK_AS_ADVANCED("${DEPNAME}_INCLUDE_DIR")
#MESSAGE( " ${DEPNAME}_INCLUDE_DIR --> ${${DEPNAME}_INCLUDE_DIR}<--")

FIND_LIBRARY("${DEPNAME}_LIBRARY" 
NAMES ${MYLIBRARY}
PATHS ${MY_PATH_LIB}
)
IF(${DEPNAME}_LIBRARY)
GET_FILENAME_COMPONENT(MYLIBNAME ${${DEPNAME}_LIBRARY} NAME_WE)
GET_FILENAME_COMPONENT(MYBINPATH ${${DEPNAME}_LIBRARY} PATH)
GET_FILENAME_COMPONENT(MYBINPATH ${MYBINPATH} PATH)
SET(MYBINPATH "${MYBINPATH}/bin")
IF(EXISTS ${MYBINPATH})
   SET(MYFOUND 0)
   FOREACH(MYPATH ${MY_ACCUM_BINARY_DEP})
       IF(MYPATH MATCHES ${MYBINPATH})
           SET(MYFOUND 1)
           #MESSAGE("found -->${MYPATH}<-->${MYBINPATH}<--")
       ENDIF(MYPATH MATCHES ${MYBINPATH})
   ENDFOREACH(MYPATH )
   IF(MYFOUND EQUAL 0)
       SET(MY_ACCUM_BINARY_DEP ${MY_ACCUM_BINARY_DEP} ${MYBINPATH})
   ENDIF(MYFOUND EQUAL 0)
ENDIF(EXISTS ${MYBINPATH})
ENDIF(${DEPNAME}_LIBRARY)



##########################################################################
MARK_AS_ADVANCED("${DEPNAME}_LIBRARY")
#MESSAGE( " ${DEPNAME}_LIBRARY --> ${${DEPNAME}_LIBRARY}<--")
IF(${DEPNAME}_INCLUDE_DIR)
IF(${DEPNAME}_LIBRARY)
SET( ${DEPNAME}_FOUND "YES" )
SET( ${DEPNAME}_LIBRARIES ${${DEPNAME}_LIBRARY} )
ENDIF(${DEPNAME}_LIBRARY)
ENDIF(${DEPNAME}_INCLUDE_DIR)
ENDMACRO(FIND_DEPENDENCY DEPNAME INCLUDEFILE LIBRARY SEARCHPATHLIST)

##############################################################################
#SET(MACRO_MESSAGE_DEBUG TRUE)
MACRO(MACRO_MESSAGE MYTEXT)
IF(MACRO_MESSAGE_DEBUG)
   MESSAGE("in file -->${CMAKE_CURRENT_LIST_FILE}<-- line -->${CMAKE_CURRENT_LIST_LINE}<-- message  ${MYTEXT}")
ELSE(MACRO_MESSAGE_DEBUG)
   MESSAGE(STATUS "in file -->${CMAKE_CURRENT_LIST_FILE}<-- line -->${CMAKE_CURRENT_LIST_LINE}<-- message  ${MYTEXT}")
ENDIF(MACRO_MESSAGE_DEBUG)
ENDMACRO(MACRO_MESSAGE MYTEXT)

MACRO(CREATE_LINK_LINES_FOR_TARGETS OUTVAR)
FOREACH(varname ${ARGN})
   IF(${varname}_RELEASE)
      IF(${varname}_DEBUG)
          Set(${OUTVAR} ${${OUTVAR}} optimized "${${varname}_RELEASE}" debug "${${varname}_DEBUG}")
      ELSE(${varname}_DEBUG)
          Set(${OUTVAR} ${${OUTVAR}} optimized "${${varname}_RELEASE}" debug "${${varname}_RELEASE}" )
      ENDIF(${varname}_DEBUG)
   ELSE(${varname}_RELEASE)
      IF(${varname}_DEBUG)
          Set(${OUTVAR} ${${OUTVAR}} optimized "${${varname}}" debug "${${varname}_DEBUG}")
      ELSE(${varname}_DEBUG)
          Set(${OUTVAR} ${${OUTVAR}} optimized "${${varname}}" debug "${${varname}}" )
      ENDIF(${varname}_DEBUG)
   ENDIF(${varname}_RELEASE)
ENDFOREACH(varname)
ENDMACRO(CREATE_LINK_LINES_FOR_TARGETS)


############################################################################
# Try to ascertain the version...
MACRO(FIND_OSG_VERSION)
IF(OSG_INCLUDE_DIR)

   SET(_osg_Version_file "${OSG_INCLUDE_DIR}/osg/Version")
   IF("${OSG_INCLUDE_DIR}" MATCHES "\\.framework$" AND NOT EXISTS "${_osg_Version_file}")
       SET(_osg_Version_file "${OSG_INCLUDE_DIR}/Headers/Version")
   ENDIF()

   IF(EXISTS "${_osg_Version_file}")
     FILE(READ "${_osg_Version_file}" _osg_Version_contents)
   ELSE()
     SET(_osg_Version_contents "unknown")
   ENDIF()

   STRING(REGEX MATCH ".*#define OSG_VERSION_MAJOR[ \t]+[0-9]+.*"
       _osg_old_defines "${_osg_Version_contents}")
   STRING(REGEX MATCH ".*#define OPENSCENEGRAPH_MAJOR_VERSION[ \t]+[0-9]+.*"
       _osg_new_defines "${_osg_Version_contents}")
   IF(_osg_old_defines)
       STRING(REGEX REPLACE ".*#define OSG_VERSION_MAJOR[ \t]+([0-9]+).*"
           "\\1" _osg_VERSION_MAJOR ${_osg_Version_contents})
       STRING(REGEX REPLACE ".*#define OSG_VERSION_MINOR[ \t]+([0-9]+).*"
           "\\1" _osg_VERSION_MINOR ${_osg_Version_contents})
       STRING(REGEX REPLACE ".*#define OSG_VERSION_PATCH[ \t]+([0-9]+).*"
           "\\1" _osg_VERSION_PATCH ${_osg_Version_contents})
   ELSEIF(_osg_new_defines)
       STRING(REGEX REPLACE ".*#define OPENSCENEGRAPH_MAJOR_VERSION[ \t]+([0-9]+).*"
           "\\1" _osg_VERSION_MAJOR ${_osg_Version_contents})
       STRING(REGEX REPLACE ".*#define OPENSCENEGRAPH_MINOR_VERSION[ \t]+([0-9]+).*"
           "\\1" _osg_VERSION_MINOR ${_osg_Version_contents})
       STRING(REGEX REPLACE ".*#define OPENSCENEGRAPH_PATCH_VERSION[ \t]+([0-9]+).*"
           "\\1" _osg_VERSION_PATCH ${_osg_Version_contents})
   ELSE()
       MESSAGE("[ FindOpenSceneGraph.cmake:${CMAKE_CURRENT_LIST_LINE} ] "
           "Failed to parse version number, please report this as a bug")
   ENDIF()

   SET(OSG_VERSION_MAJOR ${_osg_VERSION_MAJOR})
   SET(OSG_VERSION_MINOR ${_osg_VERSION_MINOR})
   SET(OSG_VERSION_PATCH ${_osg_VERSION_PATCH})

   SET(OPENSCENEGRAPH_VERSION "${_osg_VERSION_MAJOR}.${_osg_VERSION_MINOR}.${_osg_VERSION_PATCH}"
                               CACHE INTERNAL "The version of OSG which was detected")
ENDIF()
ENDMACRO(FIND_OSG_VERSION)

##########################################################################
MACRO(PROJECT_ADD_LIBRARY_PROPERTIES LIB_NAME EXPORT_SYMBOL)
if (MSVC_IDE)
 SET_TARGET_PROPERTIES(${LIB_NAME}
                PROPERTIES DEFINE_SYMBOL ${EXPORT_SYMBOL})
endif (MSVC_IDE)
ENDMACRO(PROJECT_ADD_LIBRARY_PROPERTIES)

MACRO(LINK_EXTERNAL TRGTNAME)
FOREACH(LINKLIB ${ARGN})
   TARGET_LINK_LIBRARIES(${TRGTNAME} "${LINKLIB}" )
ENDFOREACH(LINKLIB)
ENDMACRO(LINK_EXTERNAL TRGTNAME)


MACRO(LINK_WITH_DEPLIB TRGTNAME)
FOREACH(varname ${ARGN})
 IF(varname)
   IF(${varname}_DEBUG)
     SET(DEBUG_LIBRARY ${varname}_DEBUG)
   ELSE(${varname}_DEBUG)
     SET(DEBUG_LIBRARY ${varname})
   ENDIF(${varname}_DEBUG)

   IF(${varname}_RELEASE)
     SET(RELEASE_LIBRARY ${varname}_RELEASE)
   ELSE(${varname}_RELEASE)
     SET(RELEASE_LIBRARY ${varname})
   ENDIF(${varname}_RELEASE)
   #MESSAGE(${${RELEASE_LIBRARY}})

   TARGET_LINK_LIBRARIES(${TRGTNAME} optimized "${${RELEASE_LIBRARY}}" debug "${${DEBUG_LIBRARY}}")
 ENDIF(varname)
ENDFOREACH(varname)
ENDMACRO(LINK_WITH_DEPLIB)

#######################################################################
MACRO(LINK_WITH_INTERNAL TRGTNAME)
IF(CMAKE_CONFIGURATION_TYPES)
FOREACH(CONF ${CMAKE_CONFIGURATION_TYPES})
 # prepare the postfix
 STRING(TOUPPER "${CONF}" CONF)
 SET( POSTFIX "${CMAKE_${CONF}_POSTFIX}" )

 IF(NOT CMAKE24)
   TARGET_LINK_LIBRARIES(${TRGTNAME} ${ARGN})
 ELSE(NOT CMAKE24)
   FOREACH(LINKLIB ${ARGN})
     IF(MSVC)
       TARGET_LINK_LIBRARIES(${TRGTNAME} optimized "${OUTPUT_LIBDIR}/${LINKLIB}${CMAKE_RELEASE_POSTFIX}.lib" debug "${OUTPUT_LIBDIR}/${LINKLIB}${POSTFIX}.lib")
       ADD_DEPENDENCIES(${TAGTNAME} ${LIBNKLIB})
     ELSE(MSVC)
       TARGET_LINK_LIBRARIES(${TRGTNAME} optimized "${LINKLIB}${CMAKE_RELEASE_POSTFIX}" debug "${LINKLIB}${POSTFIX}")
     ENDIF(MSVC)
   ENDFOREACH(LINKLIB ${ARGN})
 ENDIF(NOT CMAKE24)
ENDFOREACH()
ELSE(CMAKE_CONFIGURATION_TYPES)
IF(NOT CMAKE24)
 TARGET_LINK_LIBRARIES(${TRGTNAME} ${ARGN})
ELSE(NOT CMAKE24)
 FOREACH(LINKLIB ${ARGN})
   MESSAGE("${LINKLIB}")
   IF(MSVC)
     TARGET_LINK_LIBRARIES(${TRGTNAME} optimized "${OUTPUT_LIBDIR}/${LINKLIB}${CMAKE_RELEASE_POSTFIX}.lib" debug "${OUTPUT_LIBDIR}/${LINKLIB}${POSTFIX}.lib")
     ADD_DEPENDENCIES(${TAGTNAME} ${LIBNKLIB})
   ELSE(MSVC)
     MESSAGE("${LINKLIB}${CMAKE_RELEASE_POSTFIX}")
     TARGET_LINK_LIBRARIES(${TRGTNAME} optimized "${LINKLIB}${CMAKE_RELEASE_POSTFIX}" debug "${LINKLIB}${POSTFIX}")
   ENDIF(MSVC)
 ENDFOREACH(LINKLIB ${ARGN})
ENDIF(NOT CMAKE24)

ENDIF(CMAKE_CONFIGURATION_TYPES)
ENDMACRO(LINK_WITH_INTERNAL)

MACRO(LINK_CORELIB_DEFAULT CORELIB_NAME)
SET(ALL_GL_LIBRARIES ${OPENGL_gl_LIBRARY})
SET(ALL_GL_LIBRARIES ${ALL_GL_LIBRARIES} ${OPENGL_egl_LIBRARY})
LINK_EXTERNAL(${CORELIB_NAME} ${ALL_GL_LIBRARIES})
SET_TARGET_PROPERTIES(${CORELIB_NAME} PROPERTIES VERSION ${YDS_VERSION} SOVERSION ${YDS_SOVERSION})
ENDMACRO(LINK_CORELIB_DEFAULT CORELIB_NAME)

####################################################################
MACRO(SETUP_LINK_LIBRARIES)
SET(TARGET_LIBRARIES ${TARGET_COMMON_LIBRARIES})

#FOREACH(LINKLIB ${TARGET_ADDED_LIBRARIES})
#  SET(TO_INSERT TRUE)
#  FOREACH(value ${TARGET_COMMON_LIBRARIES})
#    IF(${value} STREQUAL ${LINKLIB})
#      SET(TO_NSERT FALSE)
#    ENDIF(${value} STREQUAL ${LINKLIB})
#  ENDFOREACH(value ${TARGET_COMMON_LIBRARIES})
#  IF(TO_INSERT)
#    LIST(APPEND TARGET_LIBRARIES ${LINKLIB})
#  ENDIF(TO_INSERT)
#ENDFOREACH(LINKLIB)

SET(ALL_GL_LIBRARIES ${OPENGL_gl_LIBRARY})

IF( LIB_INTERNALS )
LINK_WITH_INTERNAL(${TARGET_TARGETNAME} ${LIB_INTERNALS})
ENDIF(LIB_INTERNALS)

IF( TARGET_EXTERNAL_LIBRARIES )
TARGET_LINK_LIBRARIES(${TARGET_TARGETNAME} ${TARGET_EXTERNAL_LIBRARIES})
ENDIF(TARGET_EXTERNAL_LIBRARIES)

IF(TARGET_COMMON_LIBRARIES)
LINK_WITH_DEPLIB(${TARGET_TARGETNAME} ${TARGET_COMMON_LIBRARIES})
ENDIF(TARGET_COMMON_LIBRARIES)

IF(LIB_DEPS)
LINK_WITH_DEPLIB(${TARGET_TARGETNAME} ${LIB_DEPS})
ENDIF(LIB_DEPS)

IF(MSVC)
LINK_EXTERNAL(${TARGET_TARGETNAME} ${ALL_GL_LIBRARIES})
ENDIF(MSVC)


ENDMACRO(SETUP_LINK_LIBRARIES)

##########################################################################
MACRO(SET_LABEL_AND_GROUP FOLDERNAME)
SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES FOLDER ${FOLDERNAME})
IF( TARGET_LABEL )
SET_TARGET_PROPERTIES(${TARGET_TARGETNAME} PROPERTIES PROJECT_LABEL "${TARGET_TARGETNAME}")
ENDIF(TARGET_LABEL)
ENDMACRO(SET_LABEL_AND_GROUP)


########################################################################
MACRO(PROJECT_ADD_LIBRARY LIB_NAME )
IF( NOT TARGET_NAME )
SET( TARGET_NAME ${LIB_NAME} )
ENDIF( NOT TARGET_NAME )

IF(NOT TARGET_TARGETNAME)
SET(TARGET_TARGETNAME ${LIB_NAME})
ENDIF(NOT TARGET_TARGETNAME)

SET(LIBRARY_TYPE ${ARGV1})
SET( HAS_CUDA ${ARGV2} )

if (NOT LIBRARY_TYPE OR "${LIBRARY_TYPE}" STREQUAL "FRAMEWORK")
SET(LIBRARY_TYPE SHARED)
SET(PROJECT_ALLOW_FRAMEWORK ON)
else()
SET(PROJECT_ALLOW_FRAMEWORK OFF)
endif()

# check unicode set
IF( BUILD_AT_UNICODE )
ADD_DEFINITIONS(-D_UNICODE)
ENDIF( BUILD_AT_UNICODE )

SET(CONF_DIR "")

IF ( NOT WIN32 )
SET(OLDCONF ${CMAKE_BUILD_TYPE})
STRING(TOUPPER "${OLDCONF}" CONF)
IF( "${CONF}" STREQUAL "DEBUG")
 ADD_DEFINITIONS(-DDEBUG)
 #SET(CONF_DIR "debug/")
ENDIF("${CONF}" STREQUAL "DEBUG")
ENDIF( NOT WIN32 )

IF (WIN32)
# This design only makes sense on windows because there is no bin dir for libraries.
SET(OUTPUT_BINDIR ${PROJECT_BINARY_DIR}/${CONF_DIR}bin)
ELSE(WIN32)
SET(OUTPUT_LIBDIR ${PROJECT_BINARY_DIR}/${CONF_DIR}bin)
ENDIF(WIN32)

IF(WIN32)
SET (CMAKE_LIBRARY_OUTPUT_DIRECTORY  ${OUTPUT_BINDIR})
ELSE(WIN32)
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${OUTPUT_LIBDIR}) 
SET (CMAKE_ARCHIVE_OUTPUT_DIRECTORY  ${OUTPUT_LIBDIR})
SET (CMAKE_RUNTIME_OUTPUT_DIRECTORY  ${OUTPUT_BINDIR})
ENDIF(WIN32)

FOREACH(CONF ${CMAKE_CONFIGURATION_TYPES})
SET(OLDCONF ${CONF})
STRING(TOUPPER "${CONF}" CONF)
SET("CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${CONF}" "${OUTPUT_LIBDIR}")
SET("CMAKE_RUNTIME_OUTPUT_DIRECTORY_${CONF}" "${OUTPUT_BINDIR}")
IF(WIN32)
 SET("CMAKE_LIBRARY_OUTPUT_DIRECTORY_${CONF}" "${OUTPUT_BINDIR}")
ELSE()
 SET("CMAKE_LIBRARY_OUTPUT_DIRECTORY_${CONF}" "${OUTPUT_LIBDIR}")
ENDIF()
ENDFOREACH()


IF(APPLE)
IF(HAS_CUDA)
  CUDA_ADD_LIBRARY(${TARGET_TARGETNAME} "STATIC" 
    ${LIB_PUBLIC_HEADERS}
    ${LIB_SOURCES}
    )
ELSE( HAD_CUDA)
  ADD_LIBRARY(${TARGET_TARGETNAME} "STATIC" 
    ${LIB_PUBLIC_HEADERS}
    ${LIB_SOURCES}
    )
ENDIF(HAS_CUDA)
ELSE(APPLE)
IF(HAS_CUDA)
  CUDA_ADD_LIBRARY(${TARGET_TARGETNAME} ${LIBRARY_TYPE}
    ${LIB_PUBLIC_HEADERS}
    ${LIB_SOURCES}
    )
ELSE(HAS_CUDA)
  ADD_LIBRARY(${TARGET_TARGETNAME} ${LIBRARY_TYPE}
    ${LIB_PUBLIC_HEADERS}
    ${LIB_SOURCES}
    )
ENDIF(HAS_CUDA)
ENDIF(APPLE)
IF(APPLE)
SET_TARGET_PROPERTIES(${TARGET_TARGETNAME} PROPERTIES XCODE_ATTRIBUTE_WARNING_CFLAGS "")
if (APPLE)
  SET(CMAKE_INSTALL_RPATH "${PROJECT_FRAMEWORK_INSTALL_NAME_DIR}")

  IF (PROJECT_DYLIB_INSTALL_NAME_DIR)
    SET_TARGET_PROPERTIES(${LIB_NAME} PROPERTIES
      INSTALL_NAME_DIR "${PROJECT_DYLIB_INSTALL_NAME_DIR}"
      )
  ELSE(PROJECT_DYLIB_INSTALL_NAME_DIR)
    set(CMAKE_INSTALL_RPATH "${INSTALL_INCDIR}/${LIB_NAME}")
    SET_TARGET_PROPERTIES(${LIB_NAME} PROPERTIES
      INSTALL_NAME_DIR "${INSTALL_INCDIR}/${LIB_NAME}"
      )
  ENDIF(PROJECT_DYLIB_INSTALL_NAME_DIR)
ENDIF(APPLE)
ENDIF()

#LINK_WITH_INTERNAL(${LIB_NAME} ${LIB_INTERNALS})
#LINK_WITH_DEPLIB(${LIB_NAME} ${LIB_DEPS})

SETUP_LINK_LIBRARIES()
LINK_CORELIB_DEFAULT(${LIB_NAME})

SET_LABEL_AND_GROUP($GROUP_LABEL)
#SET_LABEL_AND_GROUP("YDCore")


INCLUDE(ModuleInstall OPTIONAL)

ENDMACRO(PROJECT_ADD_LIBRARY)

###########################################################################
MACRO (PROJECT_ADD_PLUGIN LIB_NAME)
IF(NOT TARGET_TARGETNAME)
SET(TARGET_TARGETNAME "${TARGET_DEFAULT_PREFIX}${LIB_NAME}")
ENDIF(NOT TARGET_TARGETNAME)

IF(NOT TARGET_NAME)
SET(TARGET_NAME ${LIB_NAME})
ENDIF(NOT TARGET_NAME)

#put the binary into a "STAGE plugins" folder
# For each configuration (Debug, Release, MinSizeRel... and/or anything the
# user chooses)
SET(CONF_DIR "")
IF ( NOT WIN32 )
SET(OLDCONF ${CMAKE_BUILD_TYPE})
STRING(TOUPPER "${OLDCONF}" CONF)
IF("${CONF}" STREQUAL "DEBUG")
 ADD_DEFINITIONS(-DDEBUG)
 #SET(CONF_DIR "debug/")
ENDIF("${CONF}" STREQUAL "DEBUG")
ENDIF( NOT WIN32 )

IF (WIN32)
# This design only makes sense on windows because there is no bin dir for libraries.
SET(OUTPUT_BINDIR ${PROJECT_BINARY_DIR}/${CONF_DIR}bin/${PROJECT_PLUGINS_DIR})
ELSE(WIN32)
SET(OUTPUT_LIBDIR ${PROJECT_BINARY_DIR}/${CONF_DIR}bin/${PROJECT_PLUGINS_DIR})
ENDIF(WIN32)


SET (CMAKE_ARCHIVE_OUTPUT_DIRECTORY  ${OUTPUT_LIBDIR})
SET (CMAKE_RUNTIME_OUTPUT_DIRECTORY  ${OUTPUT_BINDIR})

IF(WIN32)
SET (CMAKE_LIBRARY_OUTPUT_DIRECTORY  ${OUTPUT_BINDIR})
ELSE(WIN32)
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${OUTPUT_LIBDIR}) 
ENDIF(WIN32)

FOREACH(CONF ${CMAKE_CONFIGURATION_TYPES})
SET(OLDCONF ${CONF})
# Go uppercase (DEBUG, RELEASE...)
STRING(TOUPPER "${CONF}" CONF)

SET("CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${CONF}" "${OUTPUT_LIBDIR}")
SET("CMAKE_RUNTIME_OUTPUT_DIRECTORY_${CONF}" "${OUTPUT_BINDIR}")
IF(WIN32)
 SET("CMAKE_LIBRARY_OUTPUT_DIRECTORY_${CONF}" "${OUTPUT_BINDIR}")
ELSE()
 SET("CMAKE_LIBRARY_OUTPUT_DIRECTORY_${CONF}" "${OUTPUT_LIBDIR}")
ENDIF()
ENDFOREACH()

# check unicode set
IF( BUILD_AT_UNICODE )
ADD_DEFINITIONS(-D_UNICODE)
ENDIF( BUILD_AT_UNICODE )

ADD_LIBRARY(${TARGET_TARGETNAME} "SHARED"
  ${LIB_PUBLIC_HEADERS}
  ${LIB_SOURCES}
)
IF(APPLE)
   SET_TARGET_PROPERTIES(${TARGET_TARGETNAME} PROPERTIES XCODE_ATTRIBUTE_WARNING_CFLAGS "")
ENDIF()
SETUP_LINK_LIBRARIES()

SET_LABEL_AND_GROUP($GROUP_LABEL)
#SET_LABEL_AND_GROUP("YDPlugin")

SET(INSTALL_INCDIR include)
SET(INSTALL_BINDIR bin/${PROJECT_PLUGINS_DIR})
IF(WIN32)
SET(INSTALL_LIBDIR bin/${PROJECT_PLUGINS_DIR})
SET(INSTALL_ARCHIVEDIR lib/${PROJECT_PLUGINS_DIR})
ELSE(WIN32)
SET(INSTALL_LIBDIR bin/${PROJECT_PLUGINS_DIR})
SET(INSTALL_ARCHIVEDIR bin/${PROJECT_PLUGINS_DIR})
SET(INSTALL_FRAMEWORKDIR Frameworks)
ENDIF(WIN32)

SET(HEADERS_GROUP "Header Files")

SOURCE_GROUP(
  ${HEADERS_GROUP}
  FILES ${LIB_PUBLIC_HEADERS}
)

#IF(MSVC AND OSG_MSVC_VERSIONED_DLL)
#     HANDLE_MSVC_DLL()
#  ENDIF(MSVC AND OSG_MSVC_VERSIONED_DLL)

if (APPLE)
 SET(CMAKE_INSTALL_RPATH "${PROJECT_FRAMEWORK_INSTALL_NAME_DIR}")
 
 IF (PROJECT_DYLIB_INSTALL_NAME_DIR)
     SET_TARGET_PROPERTIES(${LIB_NAME} PROPERTIES
         INSTALL_NAME_DIR "${PROJECT_DYLIB_INSTALL_NAME_DIR}"
         )
 ELSE(PROJECT_DYLIB_INSTALL_NAME_DIR)
     set(CMAKE_INSTALL_RPATH "${INSTALL_INCDIR}/${LIB_NAME}")
     SET_TARGET_PROPERTIES(${LIB_NAME} PROPERTIES
         INSTALL_NAME_DIR "${INSTALL_INCDIR}/${LIB_NAME}"
         )
 ENDIF(PROJECT_DYLIB_INSTALL_NAME_DIR)
ENDIF(APPLE)

INSTALL (
FILES        ${LIB_PUBLIC_HEADERS}
DESTINATION  ${INSTALL_INCDIR}/${LIB_NAME}
COMPONENT PROJECT-dev
)
if (MSVC)
 INSTALL(
   TARGETS ${TARGET_TARGETNAME}
   CONFIGURATIONS Release RelWithDebInfo MinSizeRel
   RUNTIME DESTINATION ${INSTALL_BINDIR} COMPONENT PROJECT
   LIBRARY DESTINATION ${INSTALL_LIBDIR} COMPONENT PROJECT
   FRAMEWORK DESTINATION ${INSTALL_FRAMEWORKDIR} COMPONENT PROJECT
   ARCHIVE DESTINATION ${INSTALL_ARCHIVEDIR} COMPONENT PROJECT-dev
 )
 INSTALL(
   TARGETS ${TARGET_TARGETNAME}
   CONFIGURATIONS Debug
   RUNTIME DESTINATION ${INSTALL_BINDIR}/ COMPONENT PROJECT
   LIBRARY DESTINATION ${INSTALL_LIBDIR}/ COMPONENT PROJECT
   FRAMEWORK DESTINATION ${INSTALL_FRAMEWORKDIR} COMPONENT PROJECT
   ARCHIVE DESTINATION ${INSTALL_ARCHIVEDIR} COMPONENT PROJECT-dev
 )
else()
 INSTALL(
   TARGETS ${TARGET_TARGETNAME}
   RUNTIME DESTINATION ${INSTALL_BINDIR} COMPONENT PROJECT
   LIBRARY DESTINATION ${INSTALL_LIBDIR} COMPONENT PROJECT
   FRAMEWORK DESTINATION ${INSTALL_FRAMEWORKDIR} COMPONENT PROJECT
   ARCHIVE DESTINATION ${INSTALL_ARCHIVEDIR} COMPONENT PROJECT-dev
 )
endif()

#make sure that the debug build is the only one that gets built in the debug folder.
IF (MSVC)
SET_TARGET_PROPERTIES(${LIB_NAME} PROPERTIES DEBUG_OUTPUT_NAME ${LIB_NAME})
ENDIF (MSVC)

ENDMACRO(PROJECT_ADD_PLUGIN)


################################################################################
MACRO(PROJECT_ADD_EXE EXE_TARGET_NAME IS_COMMANDLINE_APP)

IF( NOT TARGET_NAME )
SET(TARGET_NAME ${EXE_TARGET_NAME})
ENDIF( NOT TARGET_NAME )
IF(NOT TARGET_TARGETNAME)
SET(TARGET_TARGETNAME "${TARGET_DEFAULT_PREFIX}${TARGET_NAME}")
ENDIF(NOT TARGET_TARGETNAME)

IF( NOT TARGET_LABEL)
SET(TARGET_LABEL "${TARGET_DEFAULT_LABEL_PREFIX} ${TARGET_NAME}")
ENDIF(NOT TARGET_LABEL)

SET(CONF_DIR "")
IF ( NOT WIN32 )
SET(OLDCONF ${CMAKE_BUILD_TYPE})
STRING(TOUPPER "${OLDCONF}" CONF)
IF( "${CONF}" STREQUAL "DEBUG")
 ADD_DEFINITIONS(-DDEBUG)
#  SET(CONF_DIR "debug/")
ENDIF("${CONF}" STREQUAL "DEBUG")
ENDIF( NOT WIN32 )

IF(${IS_COMMANDLINE_APP})
ADD_EXECUTABLE(${TARGET_TARGETNAME} ${TARGET_SRC} ${TARGET_H})
ELSE(${IS_COMMANDLINE_APP})

SET( HAS_CUDA ${ARGV2} )


IF(WIN32)
IF(REQUIRE_WINMAIN_FLAG)
 SET(PLATFORM_SPECIFIC_CONTROL WIN32)
ENDIF(REQUIRE_WINMAIN_FLAG)
ENDIF(WIN32)

# check unicode set
IF( BUILD_AT_UNICODE )
ADD_DEFINITIONS(-D_UNICODE)
ENDIF( BUILD_AT_UNICODE )
IF(APPLE)
SET(MACOSX_BUNDLE_SHORT_VERSION_STRING "1.1.1")
SET(MACOSX_BUNDLE_BUNDLE_VERSION "1.1.1")
SET(MACOSX_BUNDLE_GUI_IDENTIFIER "xt.${TARGET_TARGETNAME}" )
STRING(REGEX REPLACE "_" "-" MACOSX_BUNDLE_GUI_IDENTIFIER ${MACOSX_BUNDLE_GUI_IDENTIFIER})
SET(MACOSX_BUNDLE_BUNDLE_NAME "${TARGET_NAME}" )
SET(PLATFORM_SPECIFIC_CONTROL MACOSX_BUNDLE)
ENDIF(APPLE)

IF (WIN32)
# This design only makes sense on windows because there is no bin dir for libraries.
SET(OUTPUT_BINDIR ${PROJECT_BINARY_DIR}/${CONF_DIR}bin/)
SET(OUTPUT_LIBDIR ${PROJECT_BINARY_DIR}/${CONF_DIR}lib/)
ELSE(WIN32)
SET(OUTPUT_LIBDIR ${PROJECT_BINARY_DIR}/${CONF_DIR}bin/)
SET(OUTPUT_BINDIR ${PROJECT_BINARY_DIR}/${CONF_DIR}bin/)
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${OUTPUT_LIBDIR})
SET (CMAKE_ARCHIVE_OUTPUT_DIRECTORY  ${OUTPUT_LIBDIR})
SET (CMAKE_RUNTIME_OUTPUT_DIRECTORY  ${OUTPUT_BINDIR})
ENDIF(WIN32)

IF(HAS_CUDA)
CUDA_ADD_EXECUTABLE(${TARGET_TARGETNAME} ${PLATFORM_SPECIFIC_CONTROL} ${TARGET_SRC} ${TARGET_H})
ELSE(HAS_CUDA)
ADD_EXECUTABLE(${TARGET_TARGETNAME} ${PLATFORM_SPECIFIC_CONTROL} ${TARGET_SRC} ${TARGET_H})
ENDIF(HAS_CUDA)

IF(WIN32)
 SET_TARGET_PROPERTIES(${TARGET_TARGETNAME} PROPERTIES LINK_FLAGS_DEBUG "/SUBSYSTEM:CONSOLE")
 SET_TARGET_PROPERTIES(${TARGET_TARGETNAME} PROPERTIES LINK_FLAGS_RELEASE "/SUBSYSTEM:WINDOWS /ENTRY:\"mainCRTStartup\" ")
 SET_TARGET_PROPERTIES(${TARGET_TARGETNAME} PROPERTIES LINK_FLAGS_MINSIZEREL "/SUBSYSTEM:WINDOWS /ENTRY:\"mainCRTStartup\" ")
 SET_TARGET_PROPERTIES(${TARGET_TARGETNAME} PROPERTIES LINK_FLAGS_RELWITHDEBINFO "/SUBSYSTEM:CONSOLE")
ENDIF(WIN32)

ENDIF(${IS_COMMANDLINE_APP})

#SET_TARGET_PROPERTIES(${TARGET_TARGETNAME} PROPERTIES PROJECT_LABEL "${TARGET_TARGETNAME}")
#SET_TARGET_PROPERTIES(${TARGET_TARGETNAME} PROPERTIES PROJECT_LABEL "${TARGET_LABEL}")
SET_TARGET_PROPERTIES(${TARGET_TARGETNAME} PROPERTIES OUTPUT_NAME "${TARGET_NAME}")
SET_TARGET_PROPERTIES(${TARGET_TARGETNAME} PROPERTIES DEBUG_OUTPUT_NAME "${TARGET_NAME}${CMAKE_DEBUG_POSTFIX}")
SET_TARGET_PROPERTIES(${TARGET_TARGETNAME} PROPERTIES RELEASE_OUTPUT_NAME "${TARGET_NAME}${CMAKE_RELEASE_POSTFIX}")
SET_TARGET_PROPERTIES(${TARGET_TARGETNAME} PROPERTIES RELWITHDEBINFO_OUTPUT_NAME "${TARGET_NAME}${CMAKE_RELWITHDEBINFO_POSTFIX}")
SET_TARGET_PROPERTIES(${TARGET_TARGETNAME} PROPERTIES MINSIZEREL_OUTPUT_NAME "${TARGET_NAME}${CMAKE_MINSIZEREL_POSTFIX}")

IF(APPLE)
SET_TARGET_PROPERTIES(${TARGET_TARGETNAME} PROPERTIES XCODE_ATTRIBUTE_WARNING_CFLAGS "")
ENDIF()
SETUP_LINK_LIBRARIES()

SET_LABEL_AND_GROUP($GROUP_LABEL)
IF(APPLE)
INSTALL(TARGETS ${TARGET_TARGETNAME} RUNTIME DESTINATION bin BUNDLE DESTINATION bin)
ELSE(APPLE)
INSTALL(TARGETS ${TARGET_TARGETNAME} RUNTIME DESTINATION bin COMPONENT openscenegraph  )
ENDIF(APPLE)

ENDMACRO(PROJECT_ADD_EXE)
